<?php

namespace App\User\Controllers;

use App\Http\Controllers\BaseController;
use App\User\Facades\UserInfoFacade;
use App\User\Services\UserInfoService;
use Illuminate\Http\Request;


class UserInfoController extends BaseController
{

    private $service;

    public function __construct(UserInfoService $service)
    {
        $this->service = $service;
    }

    public function index()
    {
        dd('hello!');
    }

    /**
     * 用户登录
     * */
    public function login(Request $request)
    {
        $this->validate($request, [
            'user_name' => 'required',
            'password' => 'required'
        ], [
            'user_name.required' => '账号不能为空！',
            'password.required' => '密码不能为空！'
        ]);
        $params = $request->only('user_name', 'password', 'client_type', 'machine_info');
        if(empty($params['client_type'])) {
            $params['client_type'] = 'exhibitor';
        }
        return $this->service->loginByPassword($params);
    }

    /**
     * 用户登录
     * */
    public function adminLogin(Request $request)
    {
        $this->validate($request, [
            'user_name' => 'required',
            'password' => 'required'
        ], [
            'user_name.required' => '账号不能为空！',
            'password.required' => '密码不能为空！'
        ]);
        $params = $request->only('user_name', 'password', 'client_type', 'machine_info');
        return $this->service->loginByPassword($params);
    }

    /**
     * @param Request $request
     * @return array|int|null
     * @throws \Illuminate\Validation\ValidationException
     */
    public function miniProgLogin(Request $request)
    {
        $this->validate($request, [
            'code' => 'required',
            'encrypted_data' => 'required',
            'iv' => 'required'
        ]);
        $params = $request->only('code', 'encrypted_data', 'iv');
        return $this->service->wechatOauth($params);
    }

    /**
     * 登出
     * @param Request $request
     */
    public function logout(Request $request)
    {
        $token = $request->header('token');
        return UserInfoFacade::logout($token);
    }

    /**
     * @param Request $request
     * @return UserInfoService
     */
    public function info(Request $request)
    {
        return UserInfoFacade::getUserDetail();
    }

    /**
     * 获取后台登陆用户详情
     * */
    public function getUser()
    {
        return UserInfoFacade::getUserDetail();
    }
}
